﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


public partial class Main_Administration_ManageUser : System.Web.UI.Page
{

    /*page defaults*/
    OfficeOnlineUser currentUser;

    private void AuthenticateUser()
    {
        try
        {
            currentUser = (OfficeOnlineUser)Session["CurrentUser"];
        }
        catch (Exception ex)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }

        if (currentUser == null)
        {
            //Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }
    }
    /*end page defaults*/


    protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateUser();

        if (Request.QueryString["updateuser"] == "1")
        {
            int result = UpdateUserInfo();
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was an unknown problem in updating the user, please contact the system admin." + "</response>";
                    break;
            }
        }
        else if (Request.QueryString["deleteuser"] == "1")
        {
            int result = DeleteUser();
            switch (result)
            {
                case 1:
                    form1.InnerHtml = "<response>" + "Success" + "</response>";
                    break;
                default:
                    form1.InnerHtml = "<response>" + "There was an unknown problem in deleting the user, please contact the system admin." + "</response>";
                    break;
            }
        }
        else
        {
            BindUsersTable();

            for (int i = 0; i < OfficeOnlineUser.UserTypes.Length; i++)
            {
                if (i + 1 > currentUser.UserType)
                {
                    break;
                }

                ListItem li = new ListItem(OfficeOnlineUser.UserTypes[i], (i + 1).ToString());
                ddlType.Items.Add(li);
            }
        }
    }

    private void BindUsersTable()
    {
        string command = "sp_tb_user_GetAllUsers";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        sqlCon.Open();
        SqlDataAdapter sda = new SqlDataAdapter(command,sqlCon);

        DataSet dsUsers = new DataSet();

        sda.Fill(dsUsers);

        //DataView dvUsers = new DataView(dsUsers.Tables[0]);

        sqlCon.Close();

        dgUsers.DataSource = dsUsers;

        dgUsers.DataBind();



    }

    protected void dgUsers_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                Button btnEdit = (Button)e.Item.FindControl("btnEdit");
                Label lblEdit = (Label)e.Item.FindControl("lblEdit");
                Button btnDelete = (Button)e.Item.FindControl("btnDelete");
                Label lblDelete = (Label)e.Item.FindControl("lblDelete");
                e.Item.Cells[6].Attributes.Add("usertype", e.Item.Cells[6].Text);
                int userType = Convert.ToInt16(e.Item.Cells[6].Text) - 1;
                e.Item.Cells[6].Text = OfficeOnlineUser.UserTypes[userType];
                

                string userName = DataBinder.Eval(e.Item.DataItem, "userName").ToString();
                e.Item.Cells[0].Attributes.Add("userName",userName);
                e.Item.Cells[1].Attributes.Add("userName",userName);
                e.Item.Cells[2].Attributes.Add("userName", userName);
                e.Item.Cells[3].Attributes.Add("userName", userName);
                e.Item.Cells[4].Attributes.Add("userName", userName);
                e.Item.Cells[5].Attributes.Add("userName", userName);
                e.Item.Cells[6].Attributes.Add("userName", userName);


                lblEdit.Attributes.Add("onclick", "EditUser('" + userName + "');");
                lblDelete.Attributes.Add("onclick", "DeleteUser('" + userName + "');");
                
                
                


                break;
            //case ListItemType.Footer:
                //e.Item.Cells[1].Text = "hi";
                //e.Item.BackColor = System.Drawing.Color.Red;
                //e.Item.BorderStyle = BorderStyle.None;
                //break;

        }
    }


    private int DeleteUser()
    {
        string username = Request.QueryString["username"];

        string command = "sp_tb_user_DeleteUser";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@userName"].Value = username;

        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;
    }


    private int UpdateUserInfo()
    {
        string username = Request.QueryString["username"];
        string useremail = Request.QueryString["email"];
        string userRealName = Request.QueryString["name"];
        string address = Request.QueryString["address"];
        //int age = Convert.ToInt16(Request.QueryString["age"]);
        string contactno = Request.QueryString["contactno"];
        int userType = Convert.ToInt16(Request.QueryString["usertype"]);
        string department = Request.QueryString["department"];


        string command = "sp_tb_user_UpdateUserInfo";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        SqlCommand sqlCmd = new SqlCommand(command, sqlCon);
        sqlCon.Open();
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        sqlCmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@userType", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@userEmailAddress", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@userRealName", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@userAddress", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@userContactNo", SqlDbType.NVarChar, 255, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@department", SqlDbType.NVarChar, 255, ""));
        //sqlCmd.Parameters.Add(new SqlParameter("@age", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters.Add(new SqlParameter("@result", SqlDbType.Int, 32, ""));
        sqlCmd.Parameters["@result"].Direction = ParameterDirection.Output;

        sqlCmd.Parameters["@userName"].Value = username;
        sqlCmd.Parameters["@userType"].Value = userType;
        sqlCmd.Parameters["@userEmailAddress"].Value = useremail;
        sqlCmd.Parameters["@userRealName"].Value = userRealName;
        sqlCmd.Parameters["@userAddress"].Value = address;
        sqlCmd.Parameters["@userContactNo"].Value = contactno;
        sqlCmd.Parameters["@department"].Value = department;
        //sqlCmd.Parameters["@age"].Value = age;
        sqlCmd.ExecuteNonQuery();

        int result = 0;

        try
        {
            result = Convert.ToInt16(sqlCmd.Parameters["@result"].Value);
        }
        catch (Exception ex)
        {
            result = 0;
        }

        sqlCon.Close();

        return result;
    }
    
}
